package auntie.production.app2.net;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.util.Log;
import auntie.production.app2.db.auntie_production_app_db.production_details;
import auntie.production.app2.db.auntie_production_app_db.productions;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;

public class htt_bakery {

    private static final String LOG = htt_bakery.class.getName();

    public static void htt_put_productions(List<productions> ps) {
        try {
            XStream xstream = new XStream(new JsonHierarchicalStreamDriver());
            xstream.alias("productions", List.class);
            xstream.alias("production", productions.class);

            String json = xstream.toXML(ps);

            Log.i(LOG, json);

            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("jdata", json));

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(
                    "http://192.168.2.103/put_productions");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            String ok = EntityUtils.toString(entity);
            if( !ok.equals("ok")) {
                throw new RuntimeException("error sending");
            }
            
        } catch (Exception e) {
            Log.e(LOG, "http_productions", e);
            throw new RuntimeException(e);
        }
    }
    
    public static void htt_put_production_details(List<production_details> pds) {
        try {
            XStream xstream = new XStream(new JsonHierarchicalStreamDriver());
            xstream.alias("production_details", List.class);
            xstream.alias("production_detail", production_details.class);

            String json = xstream.toXML(pds);

            Log.i(LOG, json);

            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("jdata", json));

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(
                    "http://192.168.2.103/put_production_details");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            String ok = EntityUtils.toString(entity);
            if( !ok.equals("ok")) {
                throw new RuntimeException("error sending");
            }
            
        } catch (Exception e) {
            Log.e(LOG, "http_production_details", e);
            throw new RuntimeException(e);
        }
    }
    
}
